package com.whiteape.book.utils;

import com.whiteape.book.model.type.BaseBeanType;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GreenDaoObjectConverter<T extends BaseBeanType> implements PropertyConverter<T, String> {
    @Override
    public T convertToEntityProperty(String databaseValue) {
        try {
            Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            return GsonUtils.string2Obj(databaseValue, tClass);
        }catch (Exception e) {
            return null;
        }

    }

    @Override
    public String convertToDatabaseValue(T entityProperty) {

        return GsonUtils.obj2String(entityProperty);
    }
}
